package com.wthink.hzgj.copyFile

import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.get
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.navigation.Navigation
import androidx.navigation.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.wthink.hzgj.BusApplication
import com.wthink.hzgj.R
import com.wthink.hzgj.copyFile.adapter.CameraListAdapter
import com.wthink.hzgj.copyFile.adapter.CameraListCheckAdapter
import com.wthink.hzgj.copyFile.viewmodel.CopyFileCameraViewModel
import com.wthink.hzgj.copyFile.viewmodel.CopyFileVideoViewModel
import com.wthink.hzgj.copyFile.viewmodel.CopyProgressDialogModel
import com.wthink.hzgj.data.domain.FileDetail
import com.wthink.hzgj.databinding.FragmentCopyFileCareraBinding
import com.wthink.hzgj.databinding.FragmentCopyFileVideoBinding
import com.wthink.hzgj.menu.LoginDialog
import com.wthink.hzgj.menu.LoginModel
import com.wthink.hzgj.util.`interface`.ProgrossCallBack
import com.wthink.hzgj.util.adapter.BaseViewHolder
import com.wthink.hzgj.util.adapter.OnItemClickListener
import com.wthink.hzgj.util.closeDefaultAnimator
import com.wthink.hzgj.util.setBackgroundAlpha
import com.wthink.hzgj.util.toast
import kotlinx.android.synthetic.main.item_copy_file.view.*
import java.text.FieldPosition

class CopyFileVideoFragment :Fragment(){

    private val viewModel by viewModels<CopyFileVideoViewModel>()

    private lateinit var binding: FragmentCopyFileVideoBinding

    var isSelectAll : Boolean = true

    var pathList : ArrayList<String> = ArrayList()
    var isCheckedList : ArrayList<Boolean> = ArrayList()


    private val copyProgressDialogModel by viewModels<CopyProgressDialogModel>()

    private val copyProgressDialog by lazy {
        CopyProgressDialog.make(
            BusApplication.busContext, binding.root as ViewGroup, copyProgressDialogModel,
            activity as AppCompatActivity
        )
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = DataBindingUtil.inflate(
            inflater,
            R.layout.fragment_copy_file_video,
            container,
            false
        )
        binding.viewModel = viewModel
        binding.lifecycleOwner = this
        return binding.root
    }

    @RequiresApi(Build.VERSION_CODES.O)
    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        binding.back.setOnClickListener {
            it.findNavController().popBackStack()
        }


        viewModel.start()

        viewModel.setprogressCallBack(object : ProgrossCallBack {
            override fun progress(pro: String) {
                if (pro.equals("100")) {
                    copyProgressDialog.dismiss()
                }
            }
        })

        binding.videoListView.layoutManager = LinearLayoutManager(context,
            LinearLayoutManager.VERTICAL,false)

        val bundle = arguments?.getString("path")

        if (bundle != null) {
            viewModel.queryDetail(bundle)
        }

        binding.videoListView.adapter = object : CameraListCheckAdapter(){
            override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
                super.onBindViewHolder(holder, position)
            }
        }

        binding.copy.setOnClickListener {
            pathList.clear()
            val list = viewModel.getList()
            for (fileDetail in list) {
                if (fileDetail.isChecked) {
                    pathList.add(fileDetail.path)
                }
            }
            if (pathList.size == 0) {
                toast(BusApplication.busContext,"未选中视频")
                return@setOnClickListener
            }

            viewModel.copyFile(pathList)
            copyProgressDialog.showAtLocation(
                binding.root,
                Gravity.CENTER,
                0,
                0
            )
        }

        binding.selectAll.setOnClickListener {
            pathList.clear()
            val items = viewModel.getList()

            for (item in items) {
                isCheckedList.add(item.isChecked)
            }

            // isSelectAll = isCheckedList.contains(false)

            if (isSelectAll) {
                for (fileDetail in items) {
                    fileDetail.isChecked = true
                }
                isSelectAll = false
                (binding.videoListView.adapter as CameraListCheckAdapter).notifyDataSetChanged()
            } else {
                for (fileDetail in items) {
                    fileDetail.isChecked = false
                }
                isSelectAll = true
                (binding.videoListView.adapter as CameraListCheckAdapter).notifyDataSetChanged()
            }

        }
        closeDefaultAnimator(binding.videoListView)
    }
}

